package com.aitesting.common;

import com.aitesting.constant.MethodEnum;
import com.aitesting.domain.RequestData;
import com.aitesting.domain.ResponseData;
import com.aitesting.domain.SessionData;
import io.restassured.RestAssured;
import io.restassured.config.EncoderConfig;
import io.restassured.config.HttpClientConfig;
import io.restassured.config.LogConfig;
import io.restassured.http.ContentType;
import io.restassured.http.Header;
import io.restassured.response.Response;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Data
public class HttpSession {

    private SessionData sessionData;

    private HttpSession() {
        try {
            PrintStream fileOutPutStream = new PrintStream("D:/IdeaProjects/test.log");

//            RestAssured.enableLoggingOfRequestAndResponseIfValidationFails();
            RestAssured.config = RestAssured.config()
                    .logConfig(LogConfig.logConfig().defaultStream(fileOutPutStream))
                    .encoderConfig(EncoderConfig.encoderConfig().defaultContentCharset("utf-8"))
                    .httpClient(HttpClientConfig.httpClientConfig()
                            .setParam("http.connection.timeout", 5000)
                            .setParam("http.socket.timeout", 5000)
                            .setParam("http.connection.request.timeout", 5000));
        } catch (FileNotFoundException e) {
            log.error(e.getMessage());
        }
    }

    public static HttpSession getSession() {
        return new HttpSession();
    }

    public Response request(String url, MethodEnum methodEnum, Map<String, ?> map_args){
        /*
        * TRequest对象
        * private final MethodEnum method;
        * private final String url;
        * private Map<String, String> params = new HashMap<>();
        * private Map<String, String> headers = new HashMap<>();
        * private Map<String, String> cookies = new HashMap<>();
        * private Map<String, Object> data = new HashMap<>();
         */

        //解析map_args参数
        Map<String ,?> headers = (Map<String, ?>) map_args.get("headers");
        Map<String ,?> params = (Map<String, ?>) map_args.get("params");
        Map<String ,?> formParams = (Map<String, ?>) map_args.get("data");
        Map<String ,?> cookies = (Map<String, ?>) map_args.get("cookies");

        Response response = null;
        //执行请求
        switch (methodEnum){
            case GET :
                response = RestAssured
                        .with()
                        .headers(headers)
                        .params(params)
                        .cookies(cookies)
                        .when()
                        .get(url);
                break;
            case POST:
                response = RestAssured
                        .with()
                        .headers(headers)
                        .params(params)
                        .cookies(cookies)
                        .body(formParams)
                        .contentType(ContentType.JSON)
                        .when()
                        .post(url);
                break;
            case DELETE:
                response = RestAssured
                        .with()
                        .headers(headers)
                        .params(params)
                        .cookies(cookies)
                        .when()
                        .delete(url);
                break;
            case PUT:
                response = RestAssured
                        .with()
                        .headers(headers)
                        .params(params)
                        .cookies(cookies)
                        .formParams(formParams)
                        .contentType(ContentType.JSON)
                        .when()
                        .put(url);
                break;
        }
        //获取Response对象
        Map<String, String> responseHeaders = new HashMap<>();
        //获取服务器返回的header
        assert response != null;
        for (Header header : response.getHeaders()){
            responseHeaders.put(header.getName(), header.getValue());
        }

        //封装请求数据到RequestData对象
        RequestData requestData = new RequestData(
                methodEnum,
                url,
                (Map<String, String>) headers,
                (Map<String, String>) cookies,
                formParams
        );
        //封装响应数据到ResponseData对象
        ResponseData responseData = new ResponseData(
                response.getStatusCode(),
                responseHeaders,
                response.getCookies(),
                response.getContentType(),
                response.body().asString()
        );

        //对执行后结果进行封装
        SessionData sessionData = new SessionData(requestData, responseData);

        //封装StepData对象-报告信息：请求数据、响应数据、执行时间
        //this.stepData.setSessionData(sessionData)
        //this.stepData.setSuccess(true)
        //this.stepData.setResponse_time(response.time())

        return response;
    }
}
